#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <fstream>
//工具类

class util
{
    public:
    static bool readFileContent(const std::string &filename,std::string *out)
    {
        // out->clear();
        // std::ifstream in(filename);
        // if(!in.is_open())
        // {
        //     return true;
        // }
        // std::string line;
        // while(std::getline(in,line))
        // {
        //     *out+=line;
        // }
        // in.close();
        //应该以二进制的方式来读取
        int filesize = Filesize(filename);
        if(filesize>0)
        {
            std::fstream in(filename);
            if(!in.is_open())
            {
                return false;
            }
            out->resize(filesize);
            in.read((char*)(out->c_str()),filesize);
            in.close();
        }
        else{
            return false;
        }
    }
    //从大字符串中提取小段字符串的方法
    static bool Readoneline(std::string &bigstr,std::string *out,const std::string &sep)
    {
        auto pos = bigstr.find(sep);
        if(pos == std::string::npos)
        {
            return false;
        }
        *out = bigstr.substr(0,pos);
        bigstr.erase(0,pos+sep.size());
        return true;
    }
    static int Filesize(const std::string &filename)
    {
        std::ifstream in (filename,std::ios::binary);
        if(!in.is_open())
        return -1;
        in.seekg(0,in.end);
        int filesize = in.tellg();
        in.seekg(0,in.beg);
        in.close();
        return filesize;
    }
};